<template>
  <el-dialog :visible.sync="visible" width="500px" custom-class="default-dialog password-dialog"
    :close-on-click-modal="false" @close="cancel" append-to-body>
    <div slot="title">
      <img :src="require(`@/assets/img/password.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="80px" class="dialog-form">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input size="small" v-model="form.oldPassword" show-password></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="新密码" prop="newPassword">
            <el-input size="small" v-model="form.newPassword" show-password></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input size="small" v-model="form.confirmPassword" show-password></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { validatePwd } from "@/common/js/validator.js";
export default {
  data() {
    var validateCheckPass = (rule, value, callback) => {
      if (value !== this.form.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else if (value == this.form.oldPassword) {
        callback(new Error("不能和原密码相同"));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      title: '修改密码',
      form: {},
      rules: {
        oldPassword: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
        newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' },
        { validator: validatePwd, trigger: "blur" },
        { validator: validateCheckPass, trigger: "blur" }],
        confirmPassword: [{ required: true, message: '请输入确认密码', trigger: 'blur' },
        { validator: validateCheckPass, trigger: "blur" }],
      },
      info: null
    };
  },
  methods: {
    showModal(row) {
      this.visible = true
      this.form = {
        id: row.sysUserId,
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
      this.info = row
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$api.userManageUpdatePassword(this.form).then(res => {
            if (res && res.code == 200) {
              this.$notify.success({
                title: '成功',
                message: '数据保存成功'
              });
              this.visible = false
              let userInfo = JSON.parse(localStorage.getItem('userInfo'))
              if (this.info.userGuid == userInfo.userGuid) {
                this.$store.commit("logout", "false");
                this.$router.push({ path: "/login" });
                localStorage.setItem('authToken', '')
                localStorage.setItem('userInfo', '')
              }
            }
          })
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
  }
};
</script>
<style lang="less">
.default-dialog.password-dialog {
  .el-dialog__body {
    padding-top: 50px;
    min-height: 270px;
    overflow: auto;
  }
}
</style>
